package com.feishi.core.media.opencv;

import com.feishi.core.media.opencv.util.OpenCvUtil;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.videoio.VideoCapture;
import org.opencv.videoio.VideoWriter;

import static org.opencv.highgui.HighGui.imshow;
import static org.opencv.highgui.HighGui.waitKey;
import static org.opencv.videoio.Videoio.*;

/**
 * @version 1.0
 * @auth caixq
 * @time 18-5-7 13:38
 **/
public class Videotape {
    static {
        OpenCvUtil.loadLibrary();
    }
    public static void main(String[] args) {
        VideoCapture captrue = new VideoCapture();
        captrue.open(0);
        String outFlie = "/tmp/fcq.avi";
        if (!captrue.isOpened()) {
            System.out.println("Camera Error");
        } else {
            Mat frame = new Mat();

            double r = captrue.get(CV_CAP_PROP_FPS);

            double width = captrue.get(CV_CAP_PROP_FRAME_WIDTH);
            double height = captrue.get(CV_CAP_PROP_FRAME_HEIGHT);
            Size size=new Size(width,height);
            System.out.println(size);
            System.out.println(r);
            int fourcc = VideoWriter.fourcc('X', 'V', 'I', 'D');
            VideoWriter write=new VideoWriter(outFlie, fourcc, r, size, true);
//            write.open(outFlie, -1, r, size, true);
            System.out.println(fourcc);
            boolean stop=false;
            //循环
            while (!stop)
            {
                //读取帧
                if (!captrue.read(frame))
                    break;
                imshow("Video", frame);
                //写入文件
                write.write(frame);

                if (waitKey(10) > 0)
                {
                    stop = true;
                }
            }
            //释放对象
            captrue.release();
            write.release();
        }
    }
}
